home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / stlogin4.lzh / UTMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-05  |  1.4 KB  |  75 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <utmp.h>
  6.  
  7. #define F_MSK O_RDWR|O_CREAT
  8.  
  9. void sleep(unsigned s);
  10.  
  11. int utmp_fd=0;
  12. char utmp_file[50] = UTMP_FILE; /* default */
  13.  
  14. struct utmp *getutent (void)
  15. {    static union {    struct utmp tmp;
  16.                     char buf[sizeof(struct utmp)];
  17.              } j;
  18.  
  19.     if (utmp_fd <= 0)    /* file not open yet */
  20.         if (utmp_fd = open (utmp_file, F_MSK), utmp_fd <= 0)
  21.             return NULL;
  22.     if (read (utmp_fd,j.buf,sizeof(j.buf)) < sizeof(j.buf))
  23.         return NULL;
  24.  
  25.     return &j.tmp;
  26. }
  27.  
  28. void setutent (void)
  29. {    int x;
  30.  
  31.     if (utmp_fd)
  32.         lseek(utmp_fd,0,SEEK_SET);
  33.     else    /* another application might be reading utmp */
  34.         for(x=0;x<10 &&    (utmp_fd = open (utmp_file, F_MSK)) <0;x++)
  35.             sleep(1);
  36.  
  37.     if(utmp_fd < 0)
  38.         fputs("utmp: Can't open UTMP file !!\n",stderr);
  39. }
  40.  
  41. void endutent (void)
  42. {    if (utmp_fd)
  43.     {    close (utmp_fd);
  44.         utmp_fd=0;
  45.     }
  46. }
  47.  
  48. void pututline(struct utmp *w)
  49. {    struct utmp t;
  50.     long c,s=sizeof(struct utmp);
  51.  
  52.     time(&w->ut_time);
  53.     
  54.     if(utmp_fd <= 0) setutent();
  55.     
  56.     if(utmp_fd > 0)
  57.     {    do
  58.         {    c=read(utmp_fd,&t,s);
  59.         }while(strncmp(t.ut_line,w->ut_line,strlen(t.ut_line)) && c>0);
  60.  
  61.         if(c>0)                /* entry found so we can overwrite */
  62.             lseek(utmp_fd,-s,SEEK_CUR);    /* if not then append */
  63.         write(utmp_fd,w,s);
  64.     }
  65. }
  66.  
  67. void utmpname(char *file)
  68. {    if(utmp_fd > 0)
  69.     {    close(utmp_fd);
  70.         utmp_fd=0;
  71.     }
  72.     if(strlen(file) > 49) return;
  73.     strcpy(utmp_file,file);
  74. }
  75.